<?php

namespace app\admin\controller;

use app\admin\service\WechatTargetService;
use think\facade\Request;
use app\common\Common;

// 腾讯商品库
class WechatTarget extends Base
{
    private $service;
    public function __construct(WechatTargetService $service)
    {
        parent::__construct();
        $this->service = $service;
    }
    //首页
    public function index()
    {
        return view();
    }
    //商品添加弹框
    public function info()
    {
        return view();
    }
    //商品库拉取弹框
    public function pulling_info()
    {
        return view();
    }
    //商品库添加弹框
    public function catalog_info()
    {
        return view();
    }
    //商品库查看商品弹框
    public function target_info()
    {
        return view();
    }
    //送审弹框
    public function audit_info()
    {
        return view();
    }

    //送审商品列表
    public function list(Request $request)
    {
        $params = Request::param();
        $result = $this->service->targetList($params);
        return Common::success($result);
    }
     //审核列表
     public function auditList(Request $request)
     {
         $params = Request::param();
         $result = $this->service->auditList($params);
         return Common::success($result);
     }
    //分类列表
    public function cargoryList(Request $request)
    {
        $params = Request::param();
        $result = $this->service->cargoryList($params);
        return Common::success($result);
    }
    //短剧选择框
    public function videoSelect(Request $request)
    {
        $params = Request::param();
        $data = $this->service->getVideoSelectList($params);

        return Common::success($data);
    }
    //小程序选择框
    public function appletSelect(Request $request)
    {
        $params = Request::param();
        $data = $this->service->getappletSelectList($params);

        return Common::success($data);
    }
    // 商品库下拉列表
    public function catalogSelect()
    {
        $params = Request::param();
        $data = $this->service->getCatalogSelectList($params);

        return Common::success($data);
    }
    // 添加商品
    public function add()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\WechatTarget::class)->scene('add')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->editWechatTarget($params, $this->getUserInfo());

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }
    // 修改商品
    public function edit()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\WechatTarget::class)->scene('edit')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->editWechatTarget($params, $this->getUserInfo());

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }
    //是否送审
    public function audit()
    {
        $params = Request::param();
        try {
            validate(\app\admin\validate\WechatTarget::class)->scene('detail')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }
        $result = $this->service->audit($params, $this->getUserInfo());

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }
    //详情
    public function detail()
    {
        $params = Request::param();
        try {
            validate(\app\admin\validate\WechatTarget::class)->scene('detail')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }
        $result = $this->service->detail($params, $this->getUserInfo());

        return Common::success($result);
    }
    //添加商品库数据
    public function catalogAdd()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\WechatTarget::class)->scene('catalog_add')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->editCatalog($params, $this->getUserInfo());

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }
    //修改商品库数据
    public function catalogEdit()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\WechatTarget::class)->scene('catalog_edit')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->editCatalog($params, $this->getUserInfo());

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }
    //详情
    public function catalogDetail()
    {
        $params = Request::param();
        try {
            validate(\app\admin\validate\WechatTarget::class)->scene('detail')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }
        $result = $this->service->detailCatalog($params, $this->getUserInfo());

        return Common::success($result);
    }
    //腾讯商品库列表
    public function catalogList()
    {
        $params = Request::param();
        $result = $this->service->catalogList($params);
        return Common::success($result);
    }
    //腾讯广告拉取商品库
    public function pullingCatalog()
    {
        $params = Request::param();
        $result = $this->service->pullingCatalog($params);
        return  Common::success($result);
    }
    //腾讯商品库拉取商品
    public function catalogTarget()
    {
        $params = Request::param();
        try {
            validate(\app\admin\validate\WechatTarget::class)->scene('detail')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }
        $result = $this->service->catalogTarget($params);
        return  Common::success($result);
    }
    //腾讯的审核状态
    public function examineTarget()
    {
        $params = Request::param();
        try {
            validate(\app\admin\validate\WechatTarget::class)->scene('detail')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }
        $result = $this->service->examineTarget($params);
        if (!$result) {
            return Common::fail();
        }

        return Common::success($result);
    }
    //腾讯商品库同步接口
    public function taskTarget()
    {
        $params = Request::param();
        $result = $this->service->taskTarget($params);
        header('Content-Type: application/xml');
        return $result;
    }
}
